﻿using AntdUI;
using Model;
using PrintLabel.Model;
using PrintLabel.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;

namespace PrintLabel.View
{
    public partial class SelectLotFrm : AntdUI.Window 
    {
        public StockTable selectdb { get; set; }
        private Parameter param = new Parameter();
        private Boolean isfinishcheck = false;
        public SelectLotFrm(Parameter param,Boolean isfinishcheck)
        {
            InitializeComponent();
            this.param = param;
            this.isfinishcheck = isfinishcheck;
            table1.AutoSizeColumnsMode = AntdUI.ColumnsMode.Fill; 
            var btns = new CellLink[] { new CellButton("修改") };
            table1.Columns = new AntdUI.ColumnCollection()
            {
                new AntdUI.Column ("CKMC","仓库名称"){Width="75"},
                new AntdUI.Column ("GH","缸号") { Width = "55" },
                 new AntdUI.Column ("YS","颜色") { Width = "55" }, 
                new AntdUI.Column ("houzhengli","工艺"),
                 new AntdUI.Column ("orderNum","订单号"){Width="75"},
                  new AntdUI.Column ("ContractNum","合同号") { Width = "75" },
                new AntdUI.Column ("kuanhao","款号"),
                new AntdUI.Column ("RukuNum","入库米数"),
                new AntdUI.Column ("BH","编号") { Width = "55" },
                new AntdUI.Column ("PM","品名") { Width = "95" },
                new AntdUI.Column ("CustomerPingMing","客户品名"),
                   new AntdUI.Column ("CustomerColorNum","客户颜色"),
            };
            //QueryFabric(1);
            CreateStockTree();
        }
        private void CreateStockTree()
        {
            if (param.OutsideInspection == "是")
            {
                tree1.Nodes.Add(param.OutsideInspectionPlace);
                tree1.SelectedNode = tree1.Nodes[0];
                tree1.ExpandAll();
            }
            else
            {
                var warehousenames = dbContext.dbcontext.Ado.SqlQuery<StockInfoTable>("select * from stockinfotable where mingcheng in (select distinct ckmc from stocktable where Leixing!='样布库')").Select(x => x.mingcheng).ToList();
                var fathernode = new TreeNode("所有仓库");
                foreach (var item in warehousenames)
                {
                    fathernode.Nodes.Add(new TreeNode(item));
                }
                tree1.Nodes.Add(fathernode);
                tree1.ExpandAll();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            QueryFabric(1);
        }
        private void QueryFabric(int current)
        {
            if (tree1.SelectedNode != null)
            {
                if (tree1.SelectedNode.Text != "所有仓库")
                {
                    table1.DataSource = dbContext.dbcontext.Queryable<StockTable>().Where(x => x.CKMC == tree1.SelectedNode.Text
                    && x.GH.Contains(txtdanhao.Text) ).OrderByDescending(x => x.ID).ToList();
                }
                else
                {
                    table1.DataSource = dbContext.dbcontext.Ado.SqlQuery<StockTable>($"select * from stocktable where ckmc" +
                        $" in (select mingcheng from stockinfotable where stockinfotable.Leixing!='样布库' ) and gh like '%{txtdanhao.Text}%' order by ID DESC").ToList();
                }
            }
            else
            {
                table1.DataSource = dbContext.dbcontext.Ado.SqlQuery<StockTable>($"select * from stocktable where ckmc in (select mingcheng from stockinfotable where stockinfotable.Leixing!='样布库' ) and gh like '%{txtdanhao.Text}%'  order by ID DESC").ToList();
            }
        }

        private void txtbianhao_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                QueryFabric(1);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(table1.SelectedIndex>=0)
            {
                var dbs= table1.DataSource as List<StockTable>;
                selectdb = dbs[table1.SelectedIndex - 1];
                this.Close();
            }
        }

        private void table1_CellDoubleClick(object sender, TableClickEventArgs e)
        {

        }

        private void tree1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            QueryFabric(1);
        }

        private void txtdanhao_Click(object sender, EventArgs e)
        {
            KeyboardHelper.ShowKeyboard("Number", "输入");
        }
    }
}
